package com.test.reactor.config;

import java.util.Map;

import org.springframework.boot.web.reactive.error.DefaultErrorAttributes;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;

import com.test.reactor.control.ClientController;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class GlobalErrorAttributes extends DefaultErrorAttributes {
	@Override
	public Map<String, Object> getErrorAttributes(ServerRequest request, boolean includeStackTrace) {
		Map<String, Object> map = super.getErrorAttributes(request, includeStackTrace);
		Throwable ex =  getError(request);
		log.error("global", ex);
		if (ex instanceof GlobalException) {
			GlobalException ge = (GlobalException) getError(request);
			map.put("exception", ge.getClass().getSimpleName());
			map.put("message", ge.getMessage());
			map.put("status", ge.getStatus().value());
			map.put("error", ge.getStatus().getReasonPhrase());
			return map;
		}
		map.put("exception", ex.getClass().getSimpleName());
		map.put("message", ex.getMessage());
		map.put("status", "500");
		return map;
	}
}